package com.jxbeidou.system.log.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.log.convert.LogOperateConvert;
import com.jxbeidou.system.log.entity.LogOperateDO;
import com.jxbeidou.system.log.service.LogOperateService;
import com.jxbeidou.system.log.query.LogOperateQuery;
import com.jxbeidou.system.log.vo.LogOperateVO;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.annotation.Resource;

import java.util.Arrays;
import java.util.List;

/**
* 操作日志表
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/log-operate")
@AllArgsConstructor
@Tag(name = "操作日志接口")
public class LogOperateController {
    @Resource
    private LogOperateService logOperateService;

    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public R<PageResult<LogOperateVO>> page(@RequestBody @Valid LogOperateQuery query){
        PageResult<LogOperateVO> page = logOperateService.page(query);

        return R.success(page);
    }

    @GetMapping("/{id}")
    @Operation(summary = "按照ID查询")
    public R<LogOperateVO> get(@PathVariable("id") Long id){
        LogOperateDO entity = logOperateService.getById(id);

        return R.success(LogOperateConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "新增记录")
    public R<String> save(@RequestBody LogOperateVO vo){
        logOperateService.save(vo);

        return R.saveSuccess();
    }

    @PutMapping
    @Operation(summary = "修改记录")
    public R<String> update(@RequestBody @Valid LogOperateVO vo){
        logOperateService.update(vo);

        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "按照ID删除")
    public R<String> delete(@PathVariable("id") Long id){
        logOperateService.delete(Arrays.asList(id));
        return R.deleteSuccess();
    }

    @PutMapping("delete")
    @Operation(summary = "批量删除")
    public R<String> delete(@RequestBody List<Long> idList) {
        logOperateService.delete(idList);
        return R.deleteSuccess();
    }
}